package org.moka.ioio.ioiopilot;

import org.moka.ioio.ioiopilot.gui.CommandPanel;
import org.moka.ioio.ioiopilot.gui.GraphSurface;
import org.moka.ioio.ioiopilot.input.GuiInputs;
import org.moka.ioio.ioiopilot.input.PhoneSensorInputs;
import org.moka.ioio.ioiopilot.input.RxInputs;
import org.moka.ioio.ioiopilot.output.ServoOutput;

import android.content.Context;
import android.util.Log; 
import android.widget.Button;
import android.widget.ToggleButton;
import ioio.lib.api.DigitalOutput;
import ioio.lib.api.IOIO;
import ioio.lib.api.PwmOutput;
import ioio.lib.api.exception.ConnectionLostException;

public abstract class AbstractController {


	
	// OUTPUT PINS
	protected static final int PIN_OUT_S1 = 10;
	protected static final int PIN_OUT_S2 = 11;
	protected static final int PIN_OUT_S3 = 12;
	protected static final int PIN_OUT_S4 = 13;
	
	//
	protected static final int PITCH = 0;
	protected static final int ROLL = 1;
	protected static final int YAW = 2;
	protected int[] axis = new int[3];

	
//	private IOIO ioio;
	protected CommandPanel commandPanel;
	protected GraphSurface graphPanel;
	
	//private ToggleButton button_ = null;
	protected GuiInputs guiInputs;	
	protected RxInputs rxChannels;
	protected PhoneSensorInputs phoneSensors;
	
	protected PwmOutput led;
	protected ServoOutput s1;
	protected ServoOutput s2;
	protected ServoOutput s3;
	protected ServoOutput s4;
	
	


	public AbstractController(Context context, CommandPanel commandPanel_, GraphSurface graphPanel_) throws ConnectionLostException
	{

		commandPanel = commandPanel_;		
		;
		//button_ = (ToggleButton) commandPanel.findViewById(R.id.button);
		
		
		
		// INIT INPUTS
		guiInputs = new GuiInputs(commandPanel);
		phoneSensors = new PhoneSensorInputs(context);		
		graphPanel_.setSensors(phoneSensors);
		
		
		
	}

	public void initBoardIO(IOIO ioio_) throws ConnectionLostException {
		// INIT BOARDS INPUTS
		rxChannels = new RxInputs(ioio_);
		// INIT OUTPUTS
		led = ioio_.openPwmOutput(IOIO.LED_PIN, 2);
		s1 = new ServoOutput(ioio_, PIN_OUT_S1);
		s2 = new ServoOutput(ioio_, PIN_OUT_S2);
		s3 = new ServoOutput(ioio_, PIN_OUT_S3);
		s4 = new ServoOutput(ioio_, PIN_OUT_S4);
	}

	public void readInputs() throws ConnectionLostException {
		//rxChannels.read();
		
	}
	
	public abstract void processInputToOutput();

	
	public void writeOutputs() throws ConnectionLostException {
		led.setDutyCycle(0.1f + ((float)guiInputs.value[GuiInputs.BUTTON]/1000) * 0.9f); 
		s1.write();
		s2.write();
		s3.write();
		s4.write();
		
	}
	
	
}
